<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property Funcionario $Funcionario
 */
class UsersController extends AppController {

	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('add');//, 'logout'
		$this->loadModel('Funcionario');
	}
	public function login() {
		if ($this->Auth->login()) {
			$this->redirect($this->Auth->redirect());
		} else {
			$this->Session->setFlash(__('Usuário e senha incorretos, tente novamente.'));
		}
	}
	public function logout() {
		$this->redirect($this->Auth->logout());
	}
/**
 * index method
 *
 * @return void
 */
	public function index() {
		#filter results
		$this->Filter->addFilters(
			array(
				'filter1' => array(
					'User.username' => array(
						'operator' => 'LIKE',
						'value' => array(
							'before' => '%', // opcional
							'after'  => '%'  // opcional
						)
					)
				),
				'filter2' => array(
					'Funcionario.nome' => array(
						'operator' => 'LIKE',
						'value' => array(
							'before' => '%', // opcional
							'after'  => '%'  // opcional
						)
					)
				)
			)
		);
		
		$this->Filter->setPaginate('order', 'User.created ASC'); // opcional
		$this->Filter->setPaginate('limit', 15);              // opcional
		
		// Define conditions
		$this->Filter->setPaginate('conditions', $this->Filter->getConditions());
		
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
		$this->set('user', $this->User->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved.'), 'default', array('class' => 'alert alert-success'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
			}
		}
		$funcionarios = $this->Funcionario->find('list');
		$this->set(compact('funcionarios'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
 	public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
        	if ($this->Cargo->validates()) {
	            if ($this->User->save($this->request->data)) {
	                $this->Session->setFlash(__('Usuário salvo com sucesso.'), 'default', array('class' => 'alert alert-success'));
	                $this->redirect(array('action' => 'index'));
	            } else {
	      
	                $this->Session->setFlash(__('O usuário não pode ser salvo.'), 'default', array('class' => 'alert alert-danger'));
	                
	            }
            }else {
            	$this->set('erro', $this->Cargo->invalidFields());
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
        $funcionarios = $this->Funcionario->find('list');
        $this->set(compact('funcionarios'));
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->User->delete()) {
			$this->Session->setFlash(__('The user has been deleted.'), 'default', array('class' => 'alert alert-success'));
		} else {
			$this->Session->setFlash(__('The user could not be deleted. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
		}
		return $this->redirect(array('action' => 'index'));
	}

}